<template>
  <!-- 查询栏 -->
  <!-- <div class="query">
    <QueryForm :form="form" v-model="queryData" @handleQuery="handleQuery" />
  </div> -->
  <!-- 表单栏 -->
  <div class="table">
    <BaseTable
      v-loading="tableLoading"
      :columns="columns"
      :data="dataList"
      :pager="pager"
      @handleCurrentChange="handleCurrentChange"
      @handleAction="handleAction"
    >
      <template v-slot:action>
        <el-button type="primary" @click="handleCreate">新增</el-button>
      </template>
    </BaseTable>
    <!-- 弹窗 -->
    <el-dialog :title="action == 'add' ? '新增' : '编辑'" v-model="showModal">
      <el-form
        ref="dialogForm"
        :rules="rules"
        :model="dataForm"
        label-width="100px"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="dataForm.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图片" prop="img_url">
          <UploadImage
            :limit="1"
            v-model="dataForm.img_url"
            @change="validParams('img_url')"
          />
        </el-form-item>
        <el-form-item label="跳转地址" prop="jump_url">
          <el-input v-model="dataForm.jump_url" placeholder="请输入跳转地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { nextTick, onMounted, reactive, ref, toRaw } from "vue";
import { queryBlogCarouselListByPage, blogCarouselAdd, blogCarouselDel, blogCarouselEdit } from "@/api/blogCarousel";
import UploadImage from "@/components/UploadImage.vue";
import { ElMessage } from "element-plus";

/****************************************** data ******************************************/
// 参数
const pager = reactive({
  page: 1,
  limit: 10,
  total: 0,
});

// 生成查询form结构数据
const form = [
  
];

// 表单loading
const tableLoading = ref(false);

// 初始化用户表单对象
const queryData = ref(<any>{ type: null });

// 定义动态表格的格式
const columns = reactive([
  {
    label: "标题",
    prop: "title",
  },
  {
    type: "img",
    label: "图片",
    prop: "img_url",
    width: 260,
  },
  {
    label: "跳转地址",
    prop: "jump_url",
  },
  {
    label: "请求时间",
    prop: "created_time",
    width: 180,
  },
  {
    type: "tag",
    label: "状态",
    prop: "status",
    formatter: (row: any, column: any, value: number) => {
      return {
          0: "禁用",
          1: "启用",
      }[value];
    },
    dict: {
        0: "danger", 
        1: "success", 
    },
  },
  {
    type: "switch",
    label: "状态操作",
    prop: "status",
    checked: 1,
  },
  {
    type: "action",
    label: "操作",
    width: 120,
    list: [{ text: "编辑", type: "text" }, { text: "删除",confirm: true  }],
  },
]);

// 渲染数据
const dataList = ref(<any>[]);

// 编辑动作：add,edit,delete
const action = ref("");

// 弹窗变量
const showModal = ref(false);

// 弹窗校验规则
const rules = reactive({
  title: [
    {
      required: true,
      message: "请输入标题",
      trigger: "blur",
    },
  ],
  img_url: [
    {
      required: true,
      message: "请选择图片",
      trigger: "blur",
    },
  ]
});

// 表格提交数据
const dataForm = reactive(<any>{ type: 0 });

// 弹窗ref
const dialogForm = ref(<any>null);

/****************************************** 生命周期函数 ******************************************/

onMounted(() => {
  handleQuery();
});

/****************************************** method ******************************************/

const validParams = (fieldName: String) => {
  dialogForm.value.validateField(fieldName, () => null);
};

// 查询
const handleQuery = async () => {
  tableLoading.value = true;
  let data = (await queryBlogCarouselListByPage({ ...pager, ...queryData.value })).data;
  dataList.value = data.rows;
  pager.total = data.count;
  tableLoading.value = false;
};

// 分页事件处理
const handleCurrentChange = (current: number) => {
  pager.page = current;
  handleQuery();
};

// 新增
const handleCreate = () => {
  console.log("handleCreare");
  action.value = "add";
  showModal.value = true;
};

// 操作
const handleAction = async ({ text, row }: any) => {
  if (text === "编辑") {
    console.log("编辑", row);
    action.value = "edit";
    showModal.value = true;
    await nextTick();
    Object.assign(dataForm, row);
  } else if (text === "删除") {
    console.log("删除", row);
    await blogCarouselDel({ id: row.id });
    ElMessage({ message: "删除成功", type: "success" });
    handleQuery();
  }else if (text === "状态操作") {
    let status;
    if (row.status === 1) {
      status = 0;
    } else {
      status = 1;
    }
    await blogCarouselEdit({ id: row.id, status });
    ElMessage({ message: "更新成功", type: "success" });
    handleQuery();
  }
};

// 取消
const handleClose = () => {
  console.log("取消");
  showModal.value = false;
};

// 重置
const handleReset = (form: any) => {
  form.value.resetFields();
};

// 提交
const handleSubmit = () => {
  console.log("提交");
  dialogForm.value.validate(async (valid: boolean) => {
    if (valid) {
      let res: any;
      let params = toRaw(dataForm);
      if (action.value === "add") {
        res = await blogCarouselAdd(params);
      } else if (action.value === "edit") {
        res = await blogCarouselEdit(params);
      }
      if (res && res.code === 200) {
        showModal.value = false;
        ElMessage({ message: "操作成功", type: "success" });
        handleReset(dialogForm);
        handleQuery();
      }
    }
  });
};
</script>

<style lang="scss" scoped></style>
